package com.ems.controller;

import com.ems.dao.SalaryDAO;
import com.ems.dao.EmployeeDAO;
import com.ems.model.Salary;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


public class SalaryServlet extends HttpServlet {
    private SalaryDAO salaryDAO;
    private EmployeeDAO employeeDAO;

    @Override
    public void init() {
        salaryDAO = new SalaryDAO();
        employeeDAO = new EmployeeDAO();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action") == null ? "list" : request.getParameter("action");
        try {
            switch (action) {
                case "new":
                    showNewForm(request, response);
                    break;
                case "edit":
                    showEditForm(request, response);
                    break;
                case "delete":
                    deleteSalary(request, response);
                    break;
                default:
                    listSalaries(request, response);
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action") == null ? "list" : request.getParameter("action");
        try {
            switch (action) {
                case "insert":
                    insertSalary(request, response);
                    break;
                case "update":
                    updateSalary(request, response);
                    break;
                default:
                    listSalaries(request, response);
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        }
    }

    private void listSalaries(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        List<Salary> salaries = salaryDAO.getAllSalaries();
        request.setAttribute("salaries", salaries);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/salary/list.jsp");
        dispatcher.forward(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            request.setAttribute("employees", employeeDAO.getAllEmployees());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/salary/form.jsp");
        dispatcher.forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Salary existingSalary = salaryDAO.getSalaryById(id);
        request.setAttribute("salary", existingSalary);
        request.setAttribute("employees", employeeDAO.getAllEmployees());
        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/salary/form.jsp");
        dispatcher.forward(request, response);
    }

    private void insertSalary(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        Salary newSalary = new Salary();
        populateSalaryFromRequest(request, newSalary);
        salaryDAO.addSalary(newSalary);
        response.sendRedirect("salary");
    }

    private void updateSalary(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Salary salary = salaryDAO.getSalaryById(id);
        populateSalaryFromRequest(request, salary);
        salaryDAO.updateSalary(salary);
        response.sendRedirect("salary");
    }

    private void deleteSalary(HttpServletRequest request, HttpServletResponse response)
            throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        salaryDAO.deleteSalary(id);
        response.sendRedirect("salary");
    }

    private void populateSalaryFromRequest(HttpServletRequest request, Salary salary) {
        salary.setEmployeeId(Integer.parseInt(request.getParameter("employeeId")));
        salary.setBaseSalary(Double.parseDouble(request.getParameter("baseSalary")));
        salary.setBonus(Double.parseDouble(request.getParameter("bonus")));
        salary.setDeduction(Double.parseDouble(request.getParameter("deduction")));
        // 注意：实际应用中需要处理日期转换
        salary.setPaymentDate(java.sql.Date.valueOf(request.getParameter("paymentDate")));
        salary.setStatus(request.getParameter("status"));
    }
}